package com.xcjw.com.Activity;

import android.content.Intent;
import android.graphics.Point;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.xcjw.com.Base.Myapplication;
import com.xcjw.com.Bean.LoginUser;
import com.xcjw.com.Bean.WeekPlanBean;
import com.xcjw.com.Http.HttpUrl;
import com.xcjw.com.R;
import com.xcjw.com.Utils.FileUtils;
import com.xcjw.com.Utils.JsonUtil;
import com.xcjw.com.Utils.StatusBarUtil;
import com.xcjw.com.Utils.ToastUtils;
import com.xcjw.com.View.MyListview;
import com.xcjw.com.View.ObservableScrollView;
import com.xcjw.com.View.SimpleTool;
import com.xcjw.com.View.StatusBarHeightView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Response;

/**
 * @packname com.xcjw.com.Activity
 * @filename AddNewWeekActivity
 * @date on 2018/12/5 11:26
 *****/
public class NewWeekActivity extends AppCompatActivity implements View.OnClickListener, ObservableScrollView.OnObservableScrollViewListener {

      @BindView(R.id.look)
      TextView look;
      @BindView(R.id.main_layout)
      RelativeLayout mainLayout;
      @BindView(R.id.kongtext)
      TextView kongtext;
      @BindView(R.id.listview)
      MyListview listview;
      @BindView(R.id.fabu_button)
      RelativeLayout fabuButton;
      @BindView(R.id.txt_left_title)
      TextView txtLeftTitle;
      @BindView(R.id.txt_main_title)
      TextView txtMainTitle;
      @BindView(R.id.txt_right_titleS)
      TextView txtRightTitleS;
      @BindView(R.id.txt_right_title)
      TextView txtRightTitle;
      @BindView(R.id.simple_toolbar)
      SimpleTool simpleToolbar;
      @BindView(R.id.layout)
      StatusBarHeightView layout;
      @BindView(R.id.scroll)
      ObservableScrollView scroll;
      @BindView(R.id.smart)
      SmartRefreshLayout smart;
      @BindView(R.id.image)
      ImageView image;
      @BindView(R.id.txt_left_title1)
      TextView txtLeftTitle1;
      @BindView(R.id.txt_main_title1)
      TextView txtMainTitle1;
      @BindView(R.id.txt_right_titleS1)
      TextView txtRightTitleS1;
      @BindView(R.id.txt_right_title1)
      TextView txtRightTitle1;
      @BindView(R.id.simple_toolbar1)
      SimpleTool simpleToolbar1;

      private int result;

      private List<WeekPlanBean.ContentBean> list;
      private String id;

      private WeekAdapter adapter;
      private LoginUser.ContentBean user;
      private int heights;
      private int height1;
      private Integer arrangeid;

      @Override
      protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            StatusBarUtil.setRootViewFitsSystemWindows(this, false);
            StatusBarUtil.setTranslucentStatus(this);
            setContentView(R.layout.newweek_activity);
            ButterKnife.bind(this);
            Myapplication.getInstance().addActivity(this);
            user = (LoginUser.ContentBean) FileUtils.getObject(NewWeekActivity.this, "loginUser");
            if (TextUtils.isEmpty(user.getNeighborhoodCommittees())) {
                  txtRightTitle.setText("");
                  txtRightTitle1.setText("");
            } else {
                  txtRightTitle.setText(user.getNeighborhoodCommittees());
            }
            int resourceId = this.getResources().getIdentifier("status_bar_height", "dimen", "android");
            if (resourceId > 0) {
                  result = this.getResources().getDimensionPixelSize(resourceId);
            }
            id = getIntent().getStringExtra("id");
            list = new ArrayList<>();
            WindowManager wm = this.getWindowManager();
            Point point = new Point();
            wm.getDefaultDisplay().getSize(point);
            int width = point.x;
            heights = point.y;
            adapter = new WeekAdapter();
            listview.setAdapter(adapter);
            adapter.notifyDataSetChanged();
            ViewTreeObserver viewTreeObserver = layout.getViewTreeObserver();
            viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                  @Override
                  public void onGlobalLayout() {
                        layout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                        height1 = layout.getHeight();//这里取的高度应该为图片的高度-标题栏
                        //注册滑动监听
                        scroll.setOnObservableScrollViewListener(NewWeekActivity.this);
                  }
            });

            smart.setOnRefreshListener(new OnRefreshListener() {
                  @Override
                  public void onRefresh(RefreshLayout refreshlayout) {
                        list.clear();
                        getdate();
                  }
            });

            fabuButton.setOnClickListener(this);
            txtLeftTitle.setOnClickListener(this);
            txtLeftTitle1.setOnClickListener(this);
            look.setOnClickListener(this);
      }

      @Override
      public void onClick(View v) {
            switch (v.getId()) {
                  case R.id.fabu_button:
                        Intent intent = new Intent(NewWeekActivity.this, AddWeekActivity.class);
                        intent.putExtra("id", id + "");
                        startActivity(intent);
                        break;
                  case R.id.txt_left_title:
                        onBackPressed();
                        break;
                  case R.id.txt_left_title1:
                        onBackPressed();
                        break;
                  case R.id.look:
                        Intent intent1 = new Intent(NewWeekActivity.this, LookWeekActivity.class);
                        intent1.putExtra("id", id + "");
                        startActivity(intent1);
                        break;
            }

      }

      @Override
      public void onObservableScrollViewListener(int l, int t, int oldl, int oldt) {
            System.out.println("滑动" + t + "===" + height1);
            if (t > 0 && t < height1) {
                  //顶部图处于最顶部，标题栏透明
                  simpleToolbar1.setVisibility(View.GONE);
                  image.setVisibility(View.GONE);

            } else if (t >= height1) {
                  //过顶部图区域，标题栏定色
                  image.setVisibility(View.VISIBLE);
                  RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) image.getLayoutParams();
                  params.height = result;
                  params.width = heights;
                  image.setLayoutParams(params);
                  simpleToolbar1.setVisibility(View.VISIBLE);
                  simpleToolbar1.setBackgroundColor(getResources().getColor(R.color.blue));
            }
      }

      class WeekAdapter extends BaseAdapter {

            @Override
            public int getCount() {
                  return list.size() > 0 ? list.size() : 0;
            }

            @Override
            public Object getItem(int position) {
                  return list.get(position);
            }

            @Override
            public long getItemId(int position) {
                  return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                  WeekPlanHolder holder = null;
                  if (convertView == null) {
                        holder = new WeekPlanHolder();
                        convertView = View.inflate(NewWeekActivity.this, R.layout.item_week_layout, null);
                        holder.weekday = (TextView) convertView.findViewById(R.id.week_day);
                        holder.weekdays = (TextView) convertView.findViewById(R.id.week_days);
                        holder.weeklayout = (LinearLayout) convertView.findViewById(R.id.main_layout);
                        holder.weekcontent = (TextView) convertView.findViewById(R.id.week_maincontent);
                        holder.scontent = (TextView) convertView.findViewById(R.id.scontent);
                        holder.xcontent = (TextView) convertView.findViewById(R.id.xcontent);
                        convertView.setTag(holder);
                  } else {
                        holder = (WeekPlanHolder) convertView.getTag();
                  }
                  holder.weekday.setText(list.get(position).getWeeks());
                  holder.weekdays.setText(list.get(position).getDoingtime().substring(5));
                  holder.scontent.setText(list.get(position).getScontents());
                  holder.xcontent.setText(list.get(position).getXcontents());
                  if (position == 0) {
                        holder.weeklayout.setVisibility(View.VISIBLE);
                        holder.weekcontent.setText(list.get(position).getWeekContent());
                  } else {
                        holder.weeklayout.setVisibility(View.GONE);
                  }
                  return convertView;
            }
      }

      class WeekPlanHolder {
            TextView weekday, scontent, xcontent, weekcontent,weekdays;
            LinearLayout weeklayout;

      }

      public void getdate() {
            OkHttpUtils.get(HttpUrl.Url + HttpUrl.updateweek).params("arrangeCheckId", id + "").execute(new StringCallback() {
                  @Override
                  public void onSuccess(String s, Call call, Response response) {
                        if (response.code() == 200) {
                              WeekPlanBean bean = JsonUtil.parseJsonToBean(s, WeekPlanBean.class);
                              if (bean.getCode().equals("0")) {
                                    arrangeid = (Integer) bean.getArrangeid();
                                    list.addAll(bean.getContent());
                                    adapter.notifyDataSetChanged();
                                    smart.finishRefresh();
                              } else {
                                    smart.finishRefresh();
                                    ToastUtils.shorttoast(NewWeekActivity.this, bean.getMessage());
                              }
                        } else {
                              smart.finishRefresh();
                              ToastUtils.shorttoast(NewWeekActivity.this, "网络繁忙，请稍后重试");
                        }
                        if (list.size() > 0) {
                              kongtext.setVisibility(View.GONE);
                        } else {
                              kongtext.setVisibility(View.VISIBLE);
                        }
                  }

                  @Override
                  public void onError(Call call, Response response, Exception e) {
                        smart.finishRefresh();
                        if (list.size() > 0) {
                              kongtext.setVisibility(View.GONE);
                        } else {
                              kongtext.setVisibility(View.VISIBLE);
                        }
                  }
            });

      }

      @Override
      protected void onResume() {
            super.onResume();
            list.clear();
            smart.autoRefresh();
      }
}